<?php


namespace api\web\controller;


use api\web\model\AddressModel;
use api\web\model\UserModel;

use api\web\validate\AddressValidate;
use api\web\validate\UserValidate;
use cmf\controller\RestBaseController;
use Firebase\JWT\JWT;
use Firebase\JWT\Key;
use think\Request;

class UserController extends RestBaseController
{
    public function Sms(){
        $mobile = input('mobile');
        $yzm = sendAliSms($mobile);
        if($yzm == true){
            return success('验证码发送成功');
        }else{
            return error('验证码发送失败');
        }
    }

    public function register()
    {
        $date = input('post.');
        //验证数据
        $validate = new UserValidate();
        $result = $validate->scene('add')->check($date);
        if (!$result) {
            return error($validate->getError());
        }
        $user = UserModel::where('mobile',$date['mobile'])->find();
        if($user){
            return error('重复使用该手机号注册');
        }

        $r = checkSmsCode($date['mobile'],$date['code']);
        if(!$r){
            return error('验证码验证失败');
        }
//        $randStr = createRandStr();
//        $password = md5($date['password'].$randStr);
//        $date['password'] = $password;
//        $date['salt'] = $randStr;
        $date['user_name'] = $date['mobile'];
        if (UserModel::create($date)){
            return success('注册成功');
        }else{
            return error('注册失败');
        }
    }

    public function add(Request $request)
    {

        $date = input('post.');
        //验证数据
        $validate = new UserValidate();
        $result = $validate->scene('add')->check($date);
        if (!$result) {
            return error($validate->getError());
        }
        $user = UserModel::where('id',$request->userId)->find();
        if(!$user){
            return error('该手机号未注册');
        }

        /*$r = checkSmsCode($date['mobile'],$date['code']);
        if(!$r){
            return error('验证码验证失败');
        }*/
//        $randStr = createRandStr();
//        $password = md5($date['password'].$randStr);
//        $date['password'] = $password;
//        $date['salt'] = $randStr;
        $user->mobile = $date['mobile'];
        $user->image = $date['image'];
        $user->user_name = $date['user_name'];
        $user->update_time = date('Y-m-d H:i:s');
        if ($user->save()){
            return success('添加成功');
        }else{
            return error('添加失败');
        }
    }

    public function login()
    {
        $date = input('post.');
        //验证数据
        $validate = new UserValidate();
        $result = $validate->scene('login')->check($date);
        if (!$result) {
            return error($validate->getError());
        }
        $user = UserModel::where('mobile',$date['mobile'])->find();
        if(!$user){
            return error('该手机号未注册');
        }
        $r = checkSmsCode($date['mobile'],$date['code']);
        if(!$r){
            return error('验证码验证失败');
        }
        return success('登录成功',[
            'token' => singToken($user['id'],$user['mobile'])
        ]);
    }

    public function index(Request $request)
    {

        $user = UserModel::field('image,mobile,user_name')->where('id',$request->userId)->find();
        $user['image'] = request()->domain().'/storage/'.$user['image'];
        return success('查询成功',$user);
    }

    public function address(Request $request)
    {
        $date = input('post.');
        //验证数据
        $validate = new AddressValidate();
        $result = $validate->scene('add')->check($date);
        if (!$result) {
            return error($validate->getError());
        }
        $user = AddressModel::where('address',$date['address'])->find();
        if($user){
            return error('该地址已经存在');
        }

//        $r = checkSmsCode($date['mobile'],$date['code']);
//        if(!$r){
//            return error('验证码验证失败');
//        }
//        $randStr = createRandStr();
//        $password = md5($date['password'].$randStr);
//        $date['password'] = $password;
//        $date['salt'] = $randStr;
        $date['ress_id'] = $request->userId;
        if (AddressModel::create($date)){
            return success('添加地址成功');
        }else{
            return error('添加地址失败');
        }
    }

    public function addressindex(Request $request)
    {
        $user = AddressModel::field('xm_name,mobile,area,address')->where('ress_id',$request->userId)->select();
        return success('查询成功',$user);
    }

    public function ressupdate(Request $request)
    {
        $date = input('post.');
        //验证数据
        $validate = new AddressValidate();
        $result = $validate->scene('edit')->check($date);
        if (!$result) {
            return error($validate->getError());
        }
        $user = AddressModel::where('id',$date['id'])->find();
        if(!$user){
            return error('该地址未拥有');
        }

        /*$r = checkSmsCode($date['mobile'],$date['code']);
        if(!$r){
            return error('验证码验证失败');
        }*/
//        $randStr = createRandStr();
//        $password = md5($date['password'].$randStr);
//        $date['password'] = $password;
//        $date['salt'] = $randStr;
        $user->mobile = $date['mobile'];
        $user->area = $date['area'];
        $user->xm_name = $date['xm_name'];
        $user->address = $date['address'];
        $user->update_time = date('Y-m-d H:i:s');
        if ($user->save()){
            return success('修改成功');
        }else{
            return error('修改失败');
        }
    }

    public function delete($id)
    {
        $news = AddressModel::find($id);
        if (!$news) {
            $this->error('地址不存在');
        }

        if ($news->delete()) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }

}